<?php
namespace App\Services\Postman;

use Illuminate\Support\Facades\App;
use App\Exceptions\Response\Service\ServiceException;
use App\Exceptions\Response\Service\ServiceCode;
use Zttp\Zttp;
use App\Models\Auth\TokenModel;

class CoreService
{

    /**
     * 用户登录
     *
     * @var string
     */
    const URI_PLAYER_LOGIN = '/v1/user/login';

    /**
     * 制作链接地址
     *
     * @param string $uri            
     * @return string
     */
    protected static function makeUri($uri)
    {
        return rtrim(config('app.domain.api'), '/') . $uri;
    }

    /**
     * 制作签名参数
     *
     * @param array $data            
     * @return array
     */
    protected static function makeSignParam($data)
    {
        // 制作与核心服务通信的签名
        return $data;
    }

    /**
     * POST 公共请求
     *
     * @param string $uri            
     * @param array $data            
     * @return array
     * @throws ServiceException
     */
    protected static function post($uri, $data)
    {
        // 制作签名参数
        $data = array_merge($data, self::makeSignParam($data));
        
        // 发起HTTP请求
        try {
            // 超时时间 20 秒
            $response = Zttp::timeout(20)->asFormParams()->post($uri, $data);
        } catch (\Zttp\ConnectionException $e) {
            throw new ServiceException(ServiceCode::SERVICE_API_SERVICE_REQUEST_TIMEOUT);
        }
        // HTTP错误
        if (! $response->isOk()) {
            throw new ServiceException(ServiceCode::SERVICE_API_SERVICE);
        }
        // 返回数据
        $result = $response->json();
        
        // 核心服务错误
        if ($result['resp_msg']['code'] != 200) {
            throw new ServiceException(ServiceCode::SERVICE_API_SERVICE, $result['resp_msg']['message']);
        }
        
        return $result;
    }

    /**
     * 用户登录
     *
     * @param array $data            
     * @throws ServiceException
     * @return array
     */
    public static function login($data)
    {
        // 正常情况通过核心服务获取用户token
        
        // $uri = self::makeUri(self::URI_PLAYER_LOGIN);
        
        // $result = self::post($uri, $data);
        
        // return $result['resp_data'];
        
        // 制作测试数据
        $result = self::makeTestData();
        
        // 对应角色数据
        switch ($data['mobile']) {
            case 15000001661: // 老师
                if (isset($result['data']['teacher'])) {
                    $user = $result['data']['teacher'];
                    $user['user_type'] = TokenModel::USER_TYPE_TEACHER;
                }
                break;
            case 15000001662: // 家长
                if (isset($result['data']['teacher'])) {
                    $user = $result['data']['parent'];
                    $user['user_type'] = TokenModel::USER_TYPE_PARENT;
                }
                break;
            case 15000001663: // 学生
                if (isset($result['data']['teacher'])) {
                    $user = $result['data']['student'];
                    $user['user_type'] = TokenModel::USER_TYPE_STUDENT;
                }
                break;
        }
        
        return $user;
    }

    /**
     * 制作测试数据
     *
     * @return array
     */
    private static function makeTestData()
    {
        return [
            'status' => 200,
            'data' => [
                'code' => 0,
                'teacher' => [
                    'user_id' => '1661',
                    'name' => 'example_teacher_name',
                    'token' => '0AAAA8986AXXXXXD2A3417DBF4'
                ],
                'parent' => [
                    'user_id' => '1662',
                    'name' => 'example_parent_name',
                    'token' => '0BBBB789YYYYYBD2A3417DB'
                ],
                'student' => [
                    'user_id' => '1663',
                    'name' => 'example_student_name',
                    'token' => '0CCCC789YYYYYBD2A3417DSF'
                ]
            ],
            'msg' => '',
            'debug' => [
                'stack' => [],
                'sqls' => [],
                'version' => '1.4.2'
            ]
        ];
    }
}